package com.xq.controller;

import java.util.Map;

import com.google.common.base.Strings;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.xq.model.LoginToken;
import com.xq.model.User;
import com.xq.model.XiaoQu;

public class AuthenticationInterceptor implements Interceptor {

	@Override
	public void intercept(ActionInvocation ai) {
		String loginToken = ai.getController().getPara("loginToken");
		if (!Strings.isNullOrEmpty(loginToken))
		{
			LoginToken loginTokenModel = LoginToken.dao.findByTokenId(loginToken);
			if (loginTokenModel != null)
			{
				User user = User.dao.findById(loginTokenModel.getInt("userId"));
				if (user != null)
				{
					Map<String, Object> userAttr = user.cloneAttrs();
					XiaoQu xiaoqu = XiaoQu.dao.findById(user.get("xiaoQuId"));
					userAttr.put("xiaoqu", xiaoqu.cloneAttrs());
					ai.getController().setAttr("user", userAttr);
				}
			}
		}
			
		ai.invoke();
	}


}
